﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Globalization;
using Medianamik.UI.Controls;

namespace Medianamik.UI.Web.ContentManagement
{
    public partial class ListManagement : BaseModuleEditionPage<NodeWrapper>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lnkCancel.NavigateUrl = BackURL;
                LoadBreadcrumbs();
            }
        }

        protected IEnumerable<Breadcrumb> GetBreadcrumbs()
        {
            yield return new Breadcrumb(RootNodeId.ToString(), Translator.Translate("sitemap", ReturnURL));
            if(IsEdition)
                yield return new Breadcrumb(CurrentNode.ID.ToString(), CurrentNode.DisplayName);
            
        }

        protected override string ReturnURL
        {
            get { return "ListList.aspx"; }
        }

        protected string BackURL
        {
            get
            {
                return ReturnURL + "?" + DefaultParamNames.TypeID + "=" +
                     ( TypeId.HasValue && TypeId.Value != Guid.Empty
                     ? TypeId.Value
                     : CurrentNode.NodeTypeID);
            }
        }

        public override Controls.MedianamikFormTemplate MedianamikFormTemplate
        {
            get { return MedFormTemplate; }
        }

        public override LinkButton SaveButton
        {
            get { return btnSave; }
        }

        private Guid? _rootNodeId;
        protected Guid RootNodeId
        {
            get 
            {
                if (_rootNodeId == null)
                {
                    _rootNodeId = !IsEdition 
                                ? TypeId 
                                : CurrentNode.NodeTypeID;
                }
                return _rootNodeId.Value;
            }
        }

        protected override void Save(object sender, EventArgs e)
        {
            Page.Validate(BaseMedianamikControl.DefaultValidationGroupName);

            if (IsValid)
            {
                NodeManager.SaveNode(MedianamikFormTemplate.GetFormValue(CurrentNode));
                Response.Redirect(BackURL);
            }
        }

        protected void OnNavigate(object sender, HistoryEventArgs e)
        {
            if (MyScriptManager.IsNavigating)
            {
                string nodeID = e.State.Get("NodeId");

                string script =
                        String.Format("if (confirm('{3}')) {{ $find('{4}').tryLock('{0}', 3600, function() {{ OnNavigate('{0}', '{1}'); }}, function() {{ __doPostBack('{2}', '{0}'); }});}}"
                        , nodeID
                        , backOrForward.ClientID
                        , lockResquestFailed.ClientID
                        , Translator.Translate("Medianamik_ContentManagement", "SwitchNodeEditionMessage")
                        , MedianamikFormTemplate.LockRequester.ClientID);
                

                ScriptManager.RegisterStartupScript(this, typeof(ListManagement), "backOrForwardScript", script, true);
            }
        }

        protected override void OnCultureChanged(CultureChangedEventArgs e)
        {
            base.OnCultureChanged(e);
            LoadBreadcrumbs();
            breadcrumbUpdatePanel.Update();
            RebindFieldsAndUpdate();
        }

        private void LoadBreadcrumbs()
        {
            bctFilAriane.Breadcrumbs = GetBreadcrumbs();
            bctFilAriane.DataBind();
        }

        private void RebindFieldsAndUpdate()
        {
            RebindFields();
            uptPanel.Update();
        }

        protected void LockInfoTooltip_LockDeleted(object sender, EventArgs e)
        {
            LoadBreadcrumbs();
            breadcrumbUpdatePanel.Update();
        }
    }
}
